package lily.phs.healthedu;


import lily.phs.urh.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyExpandableListAdapter extends BaseExpandableListAdapter{
	
	private Context context;
	private String[] groupItems;
	private int[] groupIcons;
	private String[][] childItems;
	private int[][] childIcons;
	
	
	public MyExpandableListAdapter(Context context,String[] groupItems,int[] groupIcons,
			String[][] childItems, int[][] childIcons)
	{
		this.context = context;
		this.groupItems = groupItems;
		this.groupIcons = groupIcons;
		this.childItems = childItems;
		this.childIcons = childIcons;
		
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	/*********************************************************************/
	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		LayoutInflater factory = LayoutInflater.from(context);
		View view = factory.inflate(R.layout.child_row, null);
		
		ChildViewHolder cHolder = new ChildViewHolder();
		//cHolder.iv_child = (ImageView)view.findViewById(R.id.iv_child);
		cHolder.tv_child = (TextView)view.findViewById(R.id.tv_child);
		
		String str = childItems[groupPosition][childPosition];
		cHolder.tv_child.setText(str);
		
		view.setBackgroundResource(R.drawable.circle_list_middle);
		
		return view;
	}
	/*********************************************************************/
	
	
	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return this.childItems[groupPosition].length;
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return this.groupItems.length;
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	/*********************************************************************/
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		int count = getGroupCount();
		
		LayoutInflater factory = LayoutInflater.from(context);
		View view = factory.inflate(R.layout.group_row, null);
		
		GroupViewHolder gHolder = new GroupViewHolder();
		gHolder.iv_group = (ImageView)view.findViewById(R.id.iv_group1);
		gHolder.tv_group = (TextView)view.findViewById(R.id.tv_group);
		
		if(groupPosition==0){
			String str1 = groupItems[0];
			gHolder.tv_group.setText(str1);
			view.setBackgroundResource(R.drawable.circle_list_top);
			Drawable db=this.context.getResources().getDrawable(groupIcons[0]);
	        //ImageUtil iu=new ImageUtil();
	        Bitmap bmp=ImageUtil.drawableToBitmap(db);
	        bmp=ImageUtil.zoomBitmap(bmp, 60, 60);
	        bmp=ImageUtil.getRoundedCornerBitmap(bmp, 10.0f);
	        gHolder.iv_group.setImageBitmap(bmp);
		}
		if(groupPosition>0&&groupPosition<count-1){
			String str2 = groupItems[groupPosition];
			gHolder.tv_group.setText(str2);
			view.setBackgroundResource(R.drawable.circle_list_middle);
			Drawable db=this.context.getResources().getDrawable(groupIcons[groupPosition]);
	        //ImageUtil iu=new ImageUtil();
	        Bitmap bmp=ImageUtil.drawableToBitmap(db);
	        bmp=ImageUtil.zoomBitmap(bmp, 60, 60);
	        bmp=ImageUtil.getRoundedCornerBitmap(bmp, 10.0f);
	        gHolder.iv_group.setImageBitmap(bmp);
			
		}
		if(groupPosition==count-1){
			String str3 = groupItems[count-1];
			gHolder.tv_group.setText(str3);
			if(isExpanded){
				view.setBackgroundResource(R.drawable.circle_list_middle);
			}
			else{
				view.setBackgroundResource(R.drawable.circle_list_bottom);
			}
			Drawable db=this.context.getResources().getDrawable(groupIcons[count-1]);
	        //ImageUtil iu=new ImageUtil();
	        Bitmap bmp=ImageUtil.drawableToBitmap(db);
	        bmp=ImageUtil.zoomBitmap(bmp, 60, 60);
	        bmp=ImageUtil.getRoundedCornerBitmap(bmp, 10.0f);
	        gHolder.iv_group.setImageBitmap(bmp);
			
		}
		
		return view;
	}
	/*********************************************************************/

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}
	
	class GroupViewHolder
	{
		ImageView iv_group;
		TextView tv_group;

		GroupViewHolder()
		{
		}
	}
	
	class ChildViewHolder
	{
		ImageView iv_child;
		TextView tv_child;

		ChildViewHolder()
		{
		}
	}

}
